﻿using System;
using System.Collections.Generic;
using WmsApp.Factory;
using WmsApp.UI.DataBinding;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormOutStoreSelect : BaseForm
    {
        public FormOutStoreSelect()
        {
            InitializeComponent();
            if (GlobalClass.RunTimeMode)
            {
                Binding.BindingGoodsCodeType(lookUpEditGoodsCodeType);
                Binding.BindingGoodsCodeType(lookUpEditGoodsCodeTypeOld);

                lookUpEditGoodsCodeTypeOld.Properties.DisplayMember = "OldGoodsCode";
                lookUpEditGoodsCodeTypeOld.Properties.ValueMember = "OldGoodsCode";

                Binding.BindProvider(lookUpEditProvider);
                Binding.BindStoreType(lookUpEditStoreType);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Text = @"库存选择";
        }

        public List<ExtendStoreGoods> ExtendStoreGoodsList { get; private set; }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            var condition = " and 1=1 ";
            if (lookUpEditGoodsCodeType.EditValue != null &&
                !string.IsNullOrEmpty(lookUpEditGoodsCodeType.EditValue.ToString()))
            {
                condition += (String.Format(" and NewGoodsCode = '{0}'", lookUpEditGoodsCodeType.EditValue));
            }

            if (lookUpEditGoodsCodeTypeOld.EditValue != null &&
                !string.IsNullOrEmpty(lookUpEditGoodsCodeTypeOld.EditValue.ToString()))
            {
                condition += (String.Format(" and OldGoodsCode = '{0}'", lookUpEditGoodsCodeTypeOld.EditValue));
            }

            if (!string.IsNullOrEmpty(textEditGoodsCodeName.Text.Trim()))
            {
                condition += (String.Format(" and NewGoodsName like '%{0}%'", textEditGoodsCodeName.Text.Trim()));
            }

            if (lookUpEditStoreType.EditValue != null && !string.IsNullOrEmpty(lookUpEditStoreType.EditValue.ToString()))
            {
                condition += (String.Format(" and StoreTypeCode = '{0}'", lookUpEditStoreType.EditValue));
            }

            // ExtVarChar1           VARCHAR(50) default '',--征用，作为供货商代码使用，当入库成功时，将入库单主表的供货商代码和名称都写入库存表
            // 覆盖原来数据，当出库时，将这个数据写入到出库单明细中
            if (lookUpEditProvider.EditValue != null && !string.IsNullOrEmpty(lookUpEditProvider.EditValue.ToString()))
            {
                condition += (String.Format(" and ExtVarChar1 = '{0}'", lookUpEditProvider.EditValue));
            }

            if (!string.IsNullOrEmpty(textEditStoreLocation.Text.Trim()))
            {
                condition += (String.Format(" and ExtVarChar8  like '%{0}%'", textEditStoreLocation.Text.Trim()));
            }

            gridControlMain.DataSource =
                WmsServiceFactory.GetServiceClient().GetExtendStoreGoodsListByCondition(condition);
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            if (gridControlMain.DataSource == null)
            {
                return;
            }

            if (gridControlMain.DataSource.GetType() == typeof (List<ExtendStoreGoods>))
            {
                var list = (List<ExtendStoreGoods>) gridControlMain.DataSource;
                ExtendStoreGoodsList = new List<ExtendStoreGoods>();
                foreach (var extendStoreGoods in list)
                {
                    if (extendStoreGoods.IsSelect)
                    {
                        ExtendStoreGoodsList.Add(extendStoreGoods);
                    }
                }
            }
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void CheckEditSelectCheckedChanged(object sender, EventArgs e)
        {
            if (gridControlMain.DataSource.GetType() == typeof (List<ExtendStoreGoods>))
            {
                var list = (List<ExtendStoreGoods>) gridControlMain.DataSource;

                if (checkEditSelect.Checked)
                {
                    foreach (var extendStoreGoods in list)
                    {
                        if (!extendStoreGoods.IsSelect)
                        {
                            extendStoreGoods.IsSelect = true;
                        }
                    }
                }
                else
                {
                    foreach (var extendStoreGoods in list)
                    {
                        if (extendStoreGoods.IsSelect)
                        {
                            extendStoreGoods.IsSelect = false;
                        }
                    }
                }
                this.gridControlMain.RefreshDataSource();
            }
        }
    }
}